package com.iw.adoms.action;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iw.adoms.entity.Goods;
import com.iw.adoms.entity.Marchandise;
import com.iw.adoms.entity.StorageBatch;
import com.iw.adoms.entity.neum.FullStatus;
import com.iw.adoms.entity.neum.Status;
import com.iw.adoms.service.MarchandiseService;
import com.iw.adoms.service.StorageBatchService;
import com.iw.core.orm.PageData;
import com.iw.core.utils.WebUtils;

@Controller
@RequestMapping("/adoms/storageBatch")
public class StorageBatchAction extends BaseAction {
	private static final Logger LOGGER = Logger.getLogger(StorageBatchAction.class);
	@Resource
	private StorageBatchService storageBatchService;
	@Resource
	private MarchandiseService marchandiseService;

	@RequestMapping("/list")
	public ModelMap list(HttpServletRequest request) {
		try {
			PageData<StorageBatch> pageData = new PageData<StorageBatch>();
			// 给pageData设置参数
			WebUtils.setPageDataParameter(request, pageData);
			pageData = storageBatchService.find(pageData);

			return new ModelMap(pageData);
		} catch (Exception e) {
			// e.printStackTrace();
			return null;
		}
	}

	@RequestMapping(value = "/new")
	public String addForm(Model model, HttpServletRequest request) {
		Integer departmentId = this.getCurrentDepartment(request).getId();
		List<Marchandise> mList = marchandiseService.getAllMarchandiseByDepartMentId(departmentId);
		model.addAttribute("mList", mList);
		return "/adoms/storageBatch/input";
	}

	@RequestMapping(value = "/edit/{id}")
	public String editForm(@PathVariable("id") Integer id, Model model) {
		StorageBatch storageBatch = storageBatchService.find(id);
		model.addAttribute(storageBatch);
		return "/adoms/storageBatch/input";
	}

	@RequestMapping(value = "/inStorage", method = RequestMethod.GET)
	public String inStorage(Model model, HttpServletRequest request) {
		Integer departmentId = this.getCurrentDepartment(request).getId();
		String hql = "from StorageBatch where department.id = ? and state != ? and state != ?";
		List<StorageBatch> batchList = storageBatchService.findList(hql, departmentId, Status.STORAGE_BATCH_DONE.getValue(),FullStatus.ZDYWC.v());
		model.addAttribute("batchList", batchList);
		return "/adoms/storageBatch/inStorage";
	}

	@RequestMapping(value = "/inStorage", method = RequestMethod.POST)
	@ResponseBody
	public boolean inStorage(Integer batch, Model model, Goods goods, Integer boxid[], Integer qrid[]) {
		try {
			return storageBatchService.inStorage(batch, model, goods, boxid, qrid);
		} catch (Exception e) {
			LOGGER.error("StorageBatchAction:" + e.getMessage());
		}
		return false;

	}

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public String save(StorageBatch storageBatch) {
		if (storageBatch.isNew()) {
			storageBatchService.save(storageBatch);
		} else {
			storageBatchService.update(storageBatch);
		}

		return "redirect:/adoms/storageBatch/list";// 重定向
	}

	@RequestMapping(value = "/delete/{id}")
	public String delete(@PathVariable("id") Integer id) {
		storageBatchService.delete(id);
		return "redirect:/adoms/storageBatch/list";// 重定向
	}

	@RequestMapping(value = "/visible/{id}")
	public String visible(@PathVariable("id") Integer id) {
		storageBatchService.visible(id);
		return "redirect:/adoms/storageBatch/list";// 重定向
	}

	@RequestMapping(value = "/unVisible/{id}")
	public String unVisible(@PathVariable("id") Integer id) {
		storageBatchService.unVisible(id);
		return "redirect:/adoms/storageBatch/list";// 重定向
	}

}
